import user from '../../utils/user'
import {
    wxfileTobase64
} from '../../utils/util'
import {
    register,
    distinguishIdCard, // 实名认证 点击身份证识别身份证信息
    getEcho, // 用户信息回显
    encryptedPhoneData
} from '../../api/app.js'
const app = getApp()
Page({
    data: {
        dataInformation: {},
        userName: '',
        idCardNo: '',
        ownIdCardBack: '',
        ownIdCardFront: '',
    },
    onLoad() {
        this.getEcho()
    },
    onShow() {},
    // 个人信息回显
    async getEcho() {
        console.log('调用啦吗')
        const result = await getEcho()
        console.log(result, 'result')
        this.setData({
            dataInformation: result,
        })
    },
    // 提交信息
    async submit() {
        // if(!this.data.dataInformation.ownIdCardFront && user.userId){
        //     app.toast('请上传身份证正面')
        //     return 0
        // }
        // if(!this.data.dataInformation.ownIdCardBack && user.userId){
        //     app.toast('请上传身份证反面')
        //     return 0
        // }
        this.register()
    },
    // 提交信息接口
    async register() {
        this.data.dataInformation.openId = user.openid
        this.data.dataInformation.userHeadUrl = user.avatarUrl
        this.data.dataInformation.bankCardIs = 1
        this.data.dataInformation.userName = this.data.dataInformation.userName.replace(/\s*/g, '')
        this.data.dataInformation.isOpenSalaryCard = Number(this.data.goRelation)
        console.log(this.data.dataInformation, 'prevPage')
        const result = await register(this.data.dataInformation)
        if (result === 'needPhoto') {
            app.toast('您的身份信息校验失败，请上传您的身份照片后重试')
            this.setData({
                isEdit: true
            })
            return
        }
        app.toast('提交身份信息成功')
        console.log(app.globalData, 'app.globalData')
        setTimeout(() => {
            if (app.globalData.homePath) {
                wx.redirectTo({
                    url: '/pages/join/join',
                })
            } else if (app.globalData.registerBack) {
                wx.redirectTo({
                    url: app.globalData.registerBack,
                })
                app.globalData.registerBack = ''
            } else {
                wx.switchTab({
                    url: '/pages/index/index'
                })
            }
        }, 1000)
        await user.reLogin()
        // setTimeout(()=>{
        //     wx.navigateBack()
        // },1000)
    },
    async onEditCardback() {
        wx.navigateTo({
            url: '/pages/demo/demo?type=' + '2',
            events: {
                idCard: async (base64) => {
                    const result = await distinguishIdCard({
                        idCard: base64.base64
                    })
                    this.setData({
                        ['dataInformation.ownIdCardBack']: result.data.url
                    })
                }
            }
        })
    },
    // 上传图片
    async onEditCard() {
        wx.navigateTo({
            url: '/pages/demo/demo?type=' + '1',
            events: {
                idCard: async (base64) => {
                    const result = await distinguishIdCard({
                        idCard: base64.base64
                    })
                    if (this.data.dataInformation.userName == '' || this.data.dataInformation.idCardNo == '') {
						if (result.data?.idCardInfo?.name) {
							this.setData({
								['dataInformation.userName']: result.data.idCardInfo.name
							})
						}
						if (result.data?.idCardInfo?.code) {
							this.setData({
								['dataInformation.idCardNo']: result.data.idCardInfo.code
							})
						}
                        this.setData({
                            ['dataInformation.ownIdCardFront']: result.data.url,
                            judgeId: false
                        })
                        return 0
                    }
                    if (result.data.idCardInfo.name == this.data.dataInformation.userName || result.data.idCardInfo.code == this.data.dataInformation.idCardNo) {
						if (result.data?.idCardInfo?.name) {
							this.setData({
								['dataInformation.userName']: result.data.idCardInfo.name
							})
						}
						if (result.data?.idCardInfo?.code) {
							this.setData({
								['dataInformation.idCardNo']: result.data.idCardInfo.code
							})
						}
                        this.setData({
                            ['dataInformation.ownIdCardFront']: result.data.url,
                            judgeId: false
                        })
                    } else {
                        app.alert('身份证照片和当前身份信息不符')
                    }
                }
            }
        })


        // const image = await wx.chooseMedia({
        //     count: 1,
        //     sizeType: ['compressed'],
        // })
        // const compressImage = await wx.compressImage({
        //     src: image.tempFiles[0].tempFilePath, // 图片路径
        //     quality: 50 // 压缩质量
        // })
        // const base64 = await wxfileTobase64(compressImage.tempFilePath)
        // const result = await distinguishIdCard({
        //     idCard: base64
        // })
        // if ('name' in result.data.idCardInfo) {
        //     if (this.data.dataInformation.userName == '' || this.data.dataInformation.idCardNo == '') {
        //         this.setData({
        //             ['dataInformation.userName']: result.data.idCardInfo.name,
        //             ['dataInformation.idCardNo']: result.data.idCardInfo.code,
        //             ['dataInformation.ownIdCardFront']: result.data.url,
        //             judgeId: false
        //         })
        //         return 0
        //     }
        //     if (result.data.idCardInfo.name == this.data.dataInformation.userName || result.data.idCardInfo.code == this.data.dataInformation.idCardNo) {
        //         this.setData({
        //             ['dataInformation.userName']: result.data.idCardInfo.name,
        //             ['dataInformation.idCardNo']: result.data.idCardInfo.code,
        //             ['dataInformation.ownIdCardFront']: result.data.url,
        //             judgeId: false
        //         })
        //     } else {
        //         app.alert('身份证照片和当前身份信息不符')
        //     }
        // } else {
        //     this.setData({
        //         ['dataInformation.ownIdCardBack']: result.data.url
        //     })
        // }
        // console.log(result, 'result')
    }
})